//页面加载完毕
$(function () {

    var receiptManagement_datagrid = $("#receiptManagement_datagrid");
    var receiptManagement_dialog = $("#receiptManagement_dialog");
    var receiptManagement_form = $("#receiptManagement_form");

    //收款管理列表初始化
    receiptManagement_datagrid.datagrid({
        url:"/receiptManagement/list.do",
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#tb",
        columns:[[
            {field:"id", hidden:true},
            {field:"stuName", title:"学员", width:80,align:'center',formatter:stuNameA},
            {field:"clazz", title:"班级", width:80,align:'center',formatter:clazzAA},
            {field:"receiptTime", title:"收款时间", width:80,align:'center'},
            {field:"receiptAmount", title:"收款金额", width:80,align:'center'},
            {field:"unpaidAmount", title:"未付金额", width:80,align:'center'},
            {field:"dictionaryItem", title:"收款类型", width:80,align:'center',formatter:receiptTypeFormatter},
            {field:"receiptEmployee", title:"收款人", width:80,formatter:receiptEmpFormatter,align:'center'},
            {field:"documentNo", title:"单据号", width:80,align:'center'},
            {field:"marketingEmployee", title:"营销人员", width:80,formatter:marketingEmpFormatter,align:'center'},
            {field:"remark", title:"备注信息", width:80,align:'center'},
            {field:"reviewStatus", title:"审核状态", width:80,formatter:stateFormatter,align:'center'}
        ]]
    });
    //end

    var cmdObj = {
        //收款管理添加
        add : function (){
            //打开弹出框
            receiptManagement_dialog.dialog("open");
            //设置标题投
            receiptManagement_dialog.dialog("setTitle", "收款管理添加");
            //清除缓存数据
            receiptManagement_form.form("clear");
            //取消只读
            $(".readOn").textbox('readonly',false);
            //取消隐藏按钮
            $('.dialog-button a:eq(0)').show();
        },
        //编辑操作
        edit : function () {
            //判断是否选中
            var row = receiptManagement_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }

            receiptManagement_dialog.dialog("open");

            //修改对话框标题
            receiptManagement_dialog.dialog("setTitle","编辑收款管理");

            //姓名
            if(row.stuName){
                row["stuName.id"] = row.stuName.id;
            }
            //收款人
            if(row.receiptEmployee){
                row["receiptEmployee.id"] = row.receiptEmployee.id;
            }
            //营销人员
            if(row.marketingEmployee){
                row["marketingEmployee.id"] = row.marketingEmployee.id;
            }
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //收款类型
            if(row.dictionaryItem){
                row["dictionaryItem.id"] = row.dictionaryItem.id;
            }

            //数据回显
            receiptManagement_form.form("load",row);

            //隐藏保存按钮
            $('.dialog-button a:eq(0)').hide();
            //设置只读
            $(".readOn").textbox('readonly',true);

        },
        //收款管理删除
        removeT : function () {
            //判断是否选中
            var row = receiptManagement_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条删除数据", "info");
                return;
            }
            if(row.reviewStatus){
                $.messager.alert('温馨提示',"已审核 不允许删除!", "info");
                return;
            }
            $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(ret){
                if (ret){
                    //确认删除
                    $.get("/receiptManagement/delete.do?id=" + row.id,function (data) {
                        if(data.success){
                            //删除成功
                            $.messager.confirm('温馨提示','删除成功!',function(ret){
                                //刷新数据
                                receiptManagement_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //收款管理添加/更新
        save : function (){
            //表单提交url
            var url = "";
            //使用id区分保存于更新操作
            var id = $("#receiptManagementId").val();
            if(id){
                //编辑
                url = "/receiptManagement/update.do";
            }else{
                //添加
                url = "/receiptManagement/save.do";
            }
            //提交表单
            receiptManagement_form.form("submit", {
                url:url,
                success:function (data) {
                    //jquery将字符串转换json对象
                    //data必须是标准的json字符串
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            receiptManagement_dialog.dialog("close");
                            //刷新数据
                            receiptManagement_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                        //关闭窗口
                        receiptManagement_dialog.dialog("close");
                        //刷新数据
                        receiptManagement_datagrid.datagrid("reload");
                    }
                }

            })
        },
        //审核
        audit : function () {
            //判断是否选中
            var row = receiptManagement_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }

            if(row.reviewStatus == 1){
                $.messager.alert('温馨提示',"已审核", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认修改该员工审核状态?',function(ret){
                if (ret){
                    //确定审核
                    $.get("/receiptManagement/audit.do?id=" + row.id,function (data) {
                        if(data.success){
                            //审核成功
                            $.messager.confirm('温馨提示','审核成功!',function(ret){
                                //刷新数据
                                receiptManagement_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //高级查询  思路: 加载收款管理列表时  将多条件查询数据一同发起请求
        searchT : function () {
            var keyword = $("#keyword").val();
            var clazzId = $("#clazzId").val();
            var auditStatus = $("#auditStatus option:selected").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //加载数据列表时 携带额外参数
            receiptManagement_datagrid.datagrid("load",{
                keyword:keyword ,
                clazzId:clazzId ,
                auditStatus:auditStatus ,
                beginDate:beginDate ,
                endDate:endDate
            })
        },
        //取消操作
        cancel : function (){
            receiptManagement_dialog.dialog("close");
        },
        //刷新
        reload : function (){
            receiptManagement_datagrid.datagrid("reload");
        }
    };

    //调用
    //给所有的按钮统一绑定事件
    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    //对话框初始化
    receiptManagement_dialog.dialog({
        width:320,
        height:380,
        buttons:"#bb",
        closable:true,
        closed:true
    });
    //end

});

//收款人格式化
function receiptEmpFormatter(value, row, index){
    return value ? value.username : "";
}

//审核状态格式化
function stateFormatter(value, row, index){
    if(value == 0){
        return "<span style='color: red'>未审核</span>"
    }
    return "<span style='color: #00ee00;'>已审核</span>";
}
//营销人员格式化
function marketingEmpFormatter(value, row, index){
    return value ? value.username : "";
}

//收款类型格式化
function receiptTypeFormatter(value, row, index){
    return value ? value.name : "";
}
//班级格式化
function clazzAA(value, row, index){
    return value ? value.name : "";
}
//学员姓名格式化
//班级格式化
function stuNameA(value, row, index){
    return value ? value.name : "";
}
